/*
 * UART.cpp
 *
 * Created: 01.05.2012 13:34:01
 *  Author: Maxim Karban <maxim.karban@gmail.com>
 */ 

#include <stdint.h>

#include <avr/io.h>
#include <avr/interrupt.h>

#include "halcyon_v1.h"
#include "uart.h"


void uartInit(bool tx_en, bool rx_en, uint16_t baud){
  if(tx_en){
    UCSR0B |= (1 << TXEN0);
    UCSR0B |= (1 << TXCIE0);
    UART_TX_DDR |= 1 << UART_TX_PIN;
  }

  if(rx_en){
    UCSR0B |= (1 << RXEN0);
    UCSR0B |= (1 << RXCIE0);
    UART_RX_DDR &= ~(1 << UART_RX_PIN);
    UART_RX_PORT |= 1 << UART_RX_PIN;
  }

  UCSR0A = 0x00;
  UCSR0B |= (0 << UCSZ02);
  
  UBRR0 = (((F_CPU) + 8UL * (baud)) / (16UL * (baud)) -1UL);
}

